import { CDPlantInfo } from "../excel/CDPlantInfo";


/**
 * 植物配置
 */
export class CEPlantInfo {
    /** 获取所有植物配置 */
    public static getAllPlants(): CDPlantInfo[] {
        if (!CDPlantInfo.datas) return [];
        return CDPlantInfo.datas.getValues();
    }

    public static getFirstPlant(): CDPlantInfo {
        return CDPlantInfo.getData(1);
    }

    /** 根据概率随机获取下一个植物 */
    public static getRandomNextPlant(): CDPlantInfo {
        if (!CDPlantInfo.datas) return null;

        const allPlants = CDPlantInfo.datas.getValues();
        if (allPlants.length === 0) return null;

        // 计算总概率
        let totalProbability = 0;
        for (let i = 0; i < allPlants.length; i++) {
            totalProbability += allPlants[i].rateNext;
        }

        // 随机选择
        const random = Math.floor(Math.random() * totalProbability);
        let currentProbability = 0;

        for (let i = 0; i < allPlants.length; i++) {
            currentProbability += allPlants[i].rateNext;
            if (random < currentProbability) {
                return allPlants[i];
            }
        }

        // 兜底返回第一个
        return allPlants[0];
    }
    /**
     * 获取植物浇水成长值
     */
    public static getPlantGrowth(plantId: number): number {
        const config = CDPlantInfo.getData(plantId);
        let minGrowth = config.minGrowth;
        let maxGrowth = config.maxGrowth;
        return Math.floor(Math.random() * (maxGrowth - minGrowth + 1)) + minGrowth;
    }
}
